<?php

namespace app\model;

use think\Model;
use think\model\relation\HasOne;

class Invoice extends Model
{
    public function invoiceSave(): array
    {
        $params = request()->post();
        $params['money'] = bcmul($params['money'], 100);
        $data = $params;
        $data['state'] = 1;
        $data['uid'] = request()->admin['id'];
        $data['max_money'] = bcmul($params['max_money'], 100);

        $this->save($data);
        return $this->toArray();
    }

    public function pageData(): array
    {
        $papram = request()->param();
        $data = $this->with(['optUser'])->where(['sid' => $papram['sid']])->order(['invoice_date' => 'desc', 'create_time' => 'desc'])->paginate(['list_rows' => $papram['limit'], 'page' => $papram['page']])->toArray();
        return ['total' => $data['total'], 'lst' => array_map(function ($item) {
            $item['money'] = bcdiv($item['money'], 100,2);
            return $item;
        },$data['data'])];
    }

    public function optUser(): HasOne
    {
        return $this->hasOne(Admin::class, 'id', 'uid');
    }
}
